//
//  ViewController.m
//  GCDDemo
//
//  Created by kingboyrang on 2021/10/29.
//  Copyright © 2021 wulanzhou. All rights reserved.
//

#import "ViewController.h"
#import "DispathQueueCreateDemo.h"
#import "DispatchApplyDemo.h"
#import "DispatchGroupDemo.h"
#import "DispatchSemaphoreDemo.h"
#import "DispathSourceTimeDemo.h"
@interface ViewController ()

@property(nonatomic,strong) DispathQueueCreateDemo *dispathQueueCreateDemo; //队列(串行+并行)
@property(nonatomic,strong) DispatchApplyDemo *dispatchApplyDemo; //循环
@property(nonatomic,strong) DispatchGroupDemo *dispatchGroupDemo; //队列组
@property(nonatomic,strong) DispatchSemaphoreDemo *dispatchSemaphoreDemo; //信号量
@property(nonatomic,strong) DispathSourceTimeDemo *dispathSourceTimeDemo; //计时器

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dispathQueueCreateDemo = [[DispathQueueCreateDemo alloc] init];
    self.dispatchApplyDemo = [[DispatchApplyDemo alloc] init];
    self.dispatchGroupDemo = [[DispatchGroupDemo alloc] init];
    self.dispatchSemaphoreDemo = [[DispatchSemaphoreDemo alloc] init];
    self.dispathSourceTimeDemo = [[DispathSourceTimeDemo alloc] init];
   
    // Do any additional setup after loading the view.
}


//1.同步+并发
- (IBAction)btn1Click:(id)sender {
    [self.dispathQueueCreateDemo syncCONCURRENTQueue];
}
//2.异步+并发
- (IBAction)btn2Click:(id)sender {
    [self.dispathQueueCreateDemo asyncCONCURRENTQueue];
}
//3.同步+串行
- (IBAction)btn3Click:(id)sender {
    [self.dispathQueueCreateDemo syncSerialQueue];
}
//4.异步+串行
- (IBAction)btn4Click:(id)sender {
    [self.dispathQueueCreateDemo asyncSerialQueue];
}
//5.遍历
- (IBAction)btn5Click:(id)sender {
    
    //[self.dispatchApplyDemo dispatchApplyTest1];
    
    [self.dispatchApplyDemo dispatchApplyTest2];
    //[self.dispatchApplyDemo dispatchApplyTest3];
}
//6.延迟执行(dispatch_after使用)
- (IBAction)btn6Click:(id)sender {
    
 NSLog(@"延迟3秒执行1");
  dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC);
  dispatch_after(timer, dispatch_get_main_queue(), ^{
      NSLog(@"延迟3秒执行2");
  });
}
//7.同步+主队列(死锁，奔溃)
- (IBAction)btn7Click:(id)sender {
    
    [self.dispathQueueCreateDemo syncMainQueue];
    
    // 非主线程调用则正常
    //[NSThread detachNewThreadSelector:@selector(syncMain) toTarget:self withObject:nil];
}

//dispatch_barrier_async栅栏方法
- (IBAction)btn8Click:(id)sender {
     [self.dispathQueueCreateDemo barrierAsyncQueue];
}

//GCD 一次性代码（只执行一次）
- (IBAction)btn9Click:(id)sender {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       // 只执行 1 次的代码（这里面默认是线程安全的）
        NSLog(@"执行一次");
    });
}

//队列组
- (IBAction)btn10Click:(id)sender {
    //1.dispatch_group使用
    //[self.dispatchGroupDemo queueGroup1];
    
    //2.dispatch_group_wait使用
    //[self.dispatchGroupDemo queueGroup2];
    
    //3.dispatch_group_enter与dispatch_group_leave使用 = dispatch_group_async
    //[self.dispatchGroupDemo queueGroup3];
    
    //4.dispatch_group_wait与dispatch_group_notify结合使用
    [self.dispatchGroupDemo queueGroup4];
}

//信号量
- (IBAction)btn11Click:(id)sender {
    
    //1.将异步执行任务转换为同步执行任务1
    //NSString *str=[self.dispatchSemaphoreDemo asyncConvertSyncTask1];
    //NSLog(@"返回结果2：%@",str);
    
    //2.将异步执行任务转换为同步执行任务2
    //[self.dispatchSemaphoreDemo asyncConvertSyncTask2];
    
    //3.保证线程安全，为线程加锁
    //[self.dispatchSemaphoreDemo queueLock];
    
    //4.通过信号量，控制任务执行
    [self.dispatchSemaphoreDemo semaphoreTaskQueue];
    
}

//计时器
- (IBAction)btn12Click:(id)sender {

    
    __weak __typeof(self) weakSelf = self;
    
    //60秒倒计时
    /**
    [self.dispathSourceTimeDemo countdownWithTimes:60 progress:^(int remainSecond) {
        NSString *strTime = [NSString stringWithFormat:@"%.2d秒后重新获取验证码",remainSecond];
        weakSelf.labShow.text = strTime;
    } finished:^{
        weakSelf.labShow.text = @"计时器完成";
    }];
     */
    
    //事件派发
    /**
    [self.dispathSourceTimeDemo eventSourceWithCompleted:^{
        NSLog(@"事件派发完成");
        weakSelf.labShow.text = @"事件派发完成";
    } event:^{
        for(int i = 1; i <= 4; i ++){
            NSLog(@"事件处理~~~~~~~~~~~~~~%d", i);
        }
    }];
    */
    
    //挂起
    //[self.dispathSourceTimeDemo suspend];
    
    //进度条例子
    [self.dispathSourceTimeDemo progressDemo];
}
@end
